<template>
  <lay-card width="1200" style="height: 950px">
      <lay-row space="20">
        <lay-col md="24">
          <lay-quote size="sm">
            <h3>报表配置编辑</h3>
          </lay-quote>
        </lay-col>
      </lay-row>
     <lay-row space="20">
        <lay-col md="6">
          <lay-button type="normal" @click="queryConfig">查询</lay-button>
          <lay-button type="normal" @click="saveConfig">保存</lay-button>
        </lay-col>
     </lay-row>
     <lay-row space="20">
        <lay-col md="24">
          <lay-textarea v-model="configData" style="height: 500px"></lay-textarea>
        </lay-col>
      </lay-row>
  </lay-card>
</template>

<script lang="ts">

export default {
  name: 'ConfigEdit',
  components: {
  }
}

</script>

<script lang="ts" setup>
import {onMounted, ref} from "vue";
import {queryConfigApi, saveConfigApi} from "../../../api/fastApi/fastApi";
import {notify} from "../../../untils/tip";
import {layer} from "@layui/layer-vue";


onMounted(()=>{
    queryConfig();
})

const method = ref('config');
const configData = ref('')
const project = ref('效能平台报表导出配置');
const projects = ref([]);
const editor = ref();
const addFlag: any = ref<boolean>(false);

const setContent = (content: string) => {
  editor.value.setValue(content);
}

const queryConfig = async () => {
    let response = await queryConfigApi({project: project.value, method: method.value});
    if(response.status === 200){
      configData.value = response.content;
      setContent(configData.value);
    }
}


const saveConfigMethod = async () => {
  const formData = new FormData();
  formData.append('project', project.value);
  formData.append('addFlag', addFlag.value);
  formData.append('content', configData.value);
  let response = await saveConfigApi(formData);
  if(response.status === 200){
      notify("success", response.message)
  }else {
      notify("failed", "保存失败！")
  }
}

const saveConfig = () => {
    layer.confirm("确定保存吗？不符合yaml格式会导致程序报错！",
        {
            btn: [
                {text:'确定', callback: (id) => {
                    saveConfigMethod();
                    layer.close(id); }
                },
                {text:'取消', callback: (id) => {
                    layer.close(id); }
                }
            ]
        }
    );
}

</script>


<style scoped>

</style>
